﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class JLTEK03DOC : DocumentBase
    {
        #region-------------Properties----------------------- 
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        } 
        public JLTEK03DOCData m_Data;
        public DataSet dsParamaters;
        public JLTEK03DOCReport m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "OpenExcel";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }
        
        #region=====vwOrder=======
        CollectionViewSource vwOrder
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwOrder");
            }
        }
        DataRow dwOrder
        {
            get
            {
                if (vwOrder.View == null)
                    return null;
                if ((DataRowView)vwOrder.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwOrder.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion
        
        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        
        public string KRS
        {
            get
            {
                return m_Data.Right("  "+ txtABPAUFKRS.Text.ToString().Trim(),2);
            }
            set
            {
                txtABPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtABPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtABPAUFLFD.Text = value;
            }
        }
        #endregion
        public JLTEK03DOC()
        {
            InitializeComponent();
            this.Name = "采购单打印";
            m_Data = new JLTEK03DOCData();
            if(app.User.Trim()=="JLTV")
            {
                KRS = "13";
                LFD = "79";
            }

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        { 
            
           
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A901101"://主面料已经审核，辅料可以采购
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        btnQuery_Click(null, null); 
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                txtABPAUFKRS.Focus();
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (!IsSupplierLoded)
                {
                    SetData.Merge(m_Data.LoadSupplier());
                    IsSupplierLoded = true;
                }
                vwLIEFDAT.View.CurrentChanged += vwLIEFDAT_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwLIEFDAT_CurrentChanged(object sender, EventArgs e)
        {
            if (dwLIEFDAT == null)
                return;

            DataSet Result = new DataSet();
            Result = m_Data.GetOrderBySupplier(dwLIEFDAT);
            SetData.EKKOPF.Clear();
            SetData.Merge(Result, false, MissingSchemaAction.Add); 
        }

  
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            if (dwInput == null)
            {
                if (LFD.Trim() == "0000000")
                    return;
                dwInput = SetData.EKKOPF.NewRow();
                dwInput["EKKMANDANT"] = "00";
                dwInput["EKKAUFKRS"] = KRS;
                dwInput["EKKAUFLFD"] = LFD;
            }
            Result = m_Data.BuildData(dwInput);
           
            try
            {
                Set4ITX.Clear();
                Set4ITX.Merge(Result, false, MissingSchemaAction.Add);
            }
            catch (Exception ex)
            {
              
            }
        } 
 
        public void btnPrint_Click(object sender, EventArgs e)
        {

            using (new LongOperation(this, "读取符合条件的记录"))
            {
                
                m_rpt = new JLTEK03DOCReport(); 
                m_rpt.dsSource = Set4ITX;  
                m_rpt.WriteExcelReport(PrnType);
            }
        }

        private void txtABPAUFLFD_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnQuery_Click(null, null);
            } 
        }
    }
}
